package com.soso.base.exception.unchecked;

import com.soso.base.util.log.Logger;

public class GeneralExceptionHandler {

	/**
	 * 
	 * @param e
	 * @throws GeneralException
	 */
	public static void handle(Throwable e) throws GeneralException {
		Throwable[] nested = {e};
		Object failed=null;
		String msg=null;
		exceptionLog(msg, nested, failed);
		throw new GeneralException(msg, nested, failed);
	}
	public static void log(Throwable e) throws GeneralException {
		Throwable[] nested = {e};
		Object failed=null;
		String msg=null;
		exceptionLog(msg, nested, failed);
	}
	/**
	 * 
	 * @param msg
	 */
	public static void handle(String msg) {
		Throwable[] nested = null;
		Object failed=null;
		exceptionLog(msg, nested, failed);
		throw new GeneralException(msg, nested, failed);
	}
	public static void log(String msg) {
		Throwable[] nested = null;
		Object failed=null;
		exceptionLog(msg, nested, failed);
	}

	/**
	 * Constructs a new exception with the specified detail message and nested
	 * <code>Throwable</code>s.
	 * 
	 * @param msg
	 *            the detail message.
	 * @param nested
	 *            the nested <code>Throwable[]</code>.
	 */
	public static void handle(String msg, Throwable[] nested) {
		Object failed=null;
		exceptionLog(msg, nested, failed);
		throw new GeneralException(msg, nested, failed);
	}
	public static void log(String msg, Throwable[] nested) {
		Object failed=null;
		exceptionLog(msg, nested, failed);
	}

	/**
	 * Constructs a new exception with the specified detail message and nested
	 * <code>Throwable</code>.
	 * 
	 * @param msg
	 *            the detail message.
	 * @param nested
	 *            the nested <code>Throwable</code>.
	 */
	public static void handle(String msg, Throwable e) {
		Throwable[] nested = {e};
		Object failed=null;
		exceptionLog(msg, nested, failed);
		throw new GeneralException(msg, nested, failed);
	}
	public static void log(String msg, Throwable e) {
		Throwable[] nested = {e};
		Object failed=null;
		exceptionLog(msg, nested, failed);
	}

	/**
	 * Constructs a new exception with the specified detail message and failed
	 * object.
	 * 
	 * @param msg
	 *            the detail message.
	 * @param failed
	 *            the failed object.
	 */
	public static void handle(String msg, Object failed) {
		Throwable[] nested = null;
		exceptionLog(msg, nested, failed);
		throw new GeneralException(msg, nested, failed);
	}
	public static void log(String msg, Object failed) {
		Throwable[] nested = null;
		exceptionLog(msg, nested, failed);
	}

	/**
	 * Constructs a new exception with the specified detail message, nested
	 * <code>Throwable</code>, and failed object.
	 * 
	 * @param msg
	 *            the detail message.
	 * @param nested
	 *            the nested <code>Throwable</code>.
	 * @param failed
	 *            the failed object.
	 */
	public static void handle(String msg, Throwable e, Object failed) {
		Throwable[] nested = { e };
		exceptionLog(msg, nested, failed);
		throw new GeneralException(msg, nested, failed);
	}
	public static void log(String msg, Throwable e, Object failed) {
		Throwable[] nested = { e };
		exceptionLog(msg, nested, failed);
	}
	
	/**
	 * Constructs a new exception with the specified detail message, nested
	 * <code>Throwable</code>s, and failed object.
	 * 
	 * @param msg
	 *            the detail message.
	 * @param nested
	 *            the nested <code>Throwable[]</code>.
	 * @param failed
	 *            the failed object.
	 */
	public static void handle(String msg, Throwable[] nested, Object failed) {
		exceptionLog(msg, nested, failed);
		throw new GeneralException(msg, nested, failed);
	}
	public static void log(String msg, Throwable[] nested, Object failed) {
		exceptionLog(msg, nested, failed);
	}


	private static void exceptionLog(String msg, Throwable[] nested, Object failed) {
		if(msg!=null)
			Logger.error(msg);
		for (Throwable e : nested) {
			for (Throwable t = e; t != null;) {
				StringBuilder sb=new StringBuilder();
				sb.append(t.getClass()).append(" ")
					.append(t.getMessage())
					.append("\n");
				StackTraceElement[] trace=t.getStackTrace();
				for (int i=0; i < trace.length; i++)
					sb.append("\tat ").append(trace[i]).append("\n");
				t = t.getCause();
				Logger.error(sb.toString());
			}
		}
	}
}
